import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/base/base_controller.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/expert/items/league_hot_expert.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/entity/live/live_detail_entity.dart';
import 'package:ball_lion/entity/match/expert_views_entity.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/server/api/home_api.dart';
import 'package:ball_lion/server/api/live_api.dart';

class LiveLogic extends BaseController with GetTickerProviderStateMixin {
  List<List<LiveDetailEntity>?> lives = [null, null, null];
  int _currentIndex = 0;
  List<ResourceEntity> carousel = [];
  List<String> tabs = ["全部", "足球直播", "篮球直播"];
  late TabController tabController =
      TabController(length: tabs.length, vsync: this);
  ScrollController refreshScroll = ApmScrollController();
  late final List<List<PlanEntity>?> _pageList =
      List.generate(tabs.length, (index) => null);
  late List<List<List<PlanEntity>>?> pages =
      List.generate(tabs.length, (index) => null);
  late List<int> loadIndex = List.generate(tabs.length, (index) => 1);
  int pageSize = 15;
  int showColumnNum = 14;
  List<bool?> hasMore = [null, null, null];
  late List<HotBoardController> boardController = List.generate(
      tabs.length, (index) => HotBoardController.live(sportsId: index));

  int get currentIndex => _currentIndex;
  set currentIndex(int value) {
    _currentIndex = value;
    update();
  }

  @override
  void onReady() {
    getData();
    getPlans(0);
    tabController.addListener(() {
      if (_pageList[tabController.index] == null) {
        getPlans(tabController.index);
      }
    });
    super.onReady();
  }

  Future getData() async {
    var res = await HomeApi.getResources("app_live");
    carousel = res.filter((item) => item.button == "lbt");
    for (var i = 0; i < tabs.length; i++) {
      lives[i] = await LiveApi.getLiveList(i);
      if (lives[i]!.length > 14) hasMore[i] = true;
    }
    update();
  }

  Future getPlans(int index) async {
    loadIndex[index] = 1;
    _pageList[index] =
        await ExpertApi.getHotPlan(loadIndex[index], pageSize, 9, index);

    pages[index] = (_pageList[index] ?? []).foldPlans;
    update();
  }

  @override
  Future getRefresh() async {
    await getData();
    boardController[currentIndex].getData();
    getPlans(currentIndex);
    update();
  }

  @override
  Future loadMore() async {
    if (loadIndex[currentIndex] == 1) {
      refreshController.finishLoad((pages[currentIndex]?.length ?? 0) <
              pageSize * loadIndex[currentIndex]
          ? IndicatorResult.noMore
          : IndicatorResult.success);
      return;
    }
    loadIndex[currentIndex] += 1;
    _pageList[currentIndex] = await LiveApi.getLivePlans(
        loadIndex[currentIndex], pageSize, currentIndex);
    pages[currentIndex]?.addAll((_pageList[currentIndex] ?? []).foldPlans);
    refreshController.finishLoad(
        (pages[currentIndex]?.length ?? 0) < pageSize * loadIndex[currentIndex]
            ? IndicatorResult.noMore
            : IndicatorResult.success);
    update();
  }

  Future doRefresh() async {
    if (refreshController.headerState?.mode.isIdle == false) return;
    refreshController.callRefresh(scrollController: refreshScroll);
  }
}
